﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using MP.LayerHelper.MP_OBS.BLL;

public partial class Member_Control_Modules : MP.Core.Authentication.Control.AuthenBase
{
    public string Module
    {
        get { return Request["m"]; }
    }

    public DataTable tblDepart;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadDepart();
            loadCombo();
            loadData();
        }
    }

    void loadCombo()
    {
        DataTable dst = MbModulesManager.CreateInstant().SelectByParentCodeRDT(null);
        dst.DefaultView.Sort = "ModName";
        ddlModules.DataTextField = "ModName";
        ddlModules.DataValueField = "ModCode";
        ddlModules.DataSource = dst.DefaultView;
        ddlModules.DataBind();

        ddlModules.SelectedValue = Module;
    }

    void loadData()
    {
        if (!string.IsNullOrEmpty(Module))
        {
            DataTable tbl = MbModulesManager.CreateInstant().SelectByParentCodeRDT(Module);
            rep.DataSource = tbl;
            rep.DataBind();
        }
    }

    void loadDepart()
    {
        DataSet dst = MaDepartmentManager.CreateInstant().GetAllDepart();
        if (dst != null && dst.Tables.Count > 0)
            tblDepart = dst.Tables[0];
    }

    protected void rep_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        DataRowView row = (DataRowView)e.Item.DataItem;
        DropDownList ddl = (DropDownList)e.Item.FindControl("ddlDepart");
        ddl.SelectedValue = row["DepartId"].ToString();
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (rep.Items.Count > 0)
        {
            for (int i = 0; i < rep.Items.Count; i++)
            {
                string ModCode = ((HiddenField)rep.Items[i].FindControl("hidModCode")).Value;
                Guid DepartId = new Guid(((DropDownList)rep.Items[i].FindControl("ddlDepart")).SelectedValue);
                CheckBox chkEnable = (CheckBox)rep.Items[i].FindControl("chkEnable");

                MbModulesManager.CreateInstant().UpdateDepart(ModCode, DepartId, chkEnable.Checked);
            }

            Response.Redirect(Request.RawUrl);
        }
    }
}
